// ==UserScript==
// @name         Ctrip代理通助手
// @namespace    http://tampermonkey.net/
// @version      0.4
// @version      ╮(╯▽╰)╭
// @description  快速查找我方套餐,修复bug,最后更新于 2021-3-12 09:16:05
// @updateURL    https://gitee.com/amutravel/ctrip/raw/master/hotel/ctripHelper.user.js
// @downloadURL  https://gitee.com/amutravel/ctrip/raw/master/hotel/ctripHelper.user.js
// @match        *://www.vipdlt.com/mip/RoomManagement/MIP/RoomManage.aspx*
// @match        *://www.vipdlt.com/dltplatform/page/room/roomCalendar*
// @match        *://hotels.ctrip.com/*
// @require      https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js
// @noframes
// @run-at       document-end
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_notification
// @grant        GM_registerMenuCommand
// require      file:///D:\nut\codes\ctrip\hotel\ctripHelper.user.js
// ==/UserScript==
/* jshint esversion: 6 */
;
Zepto(function($){
  // 获取母酒店ID和子酒店ID，子酒店id就是房型管理模块里面的id
  // https://www.vipdlt.com/mip/RoomManagement/MIP/RoomManage.aspx?hotel=60980750  60980750 就是子酒店ID
  function getMotherAndChildHotelId(){
    return new Promise(resolve=>{
      resolve({
        mother: $('#h3HotelName').attr('masterhotelid'),
        child: $('#h3HotelName').attr('hotelid')
      })
    })
  }

  // 获取所有套餐的[房型ID]
  function getAllRoomIds(){
    return new Promise(resolve=>{
      let roomIds = [];
      $.each($('tr.sale-room-tr'), (index, item) => {
        roomIds.push($(item).attr('roomid'));
      })
      resolve(roomIds)
    })
  }
  // 更新/存储 某个酒店所有的房型ID
  async function updateHotelRoomids(){
    let hotelIds = await getMotherAndChildHotelId()
    let roomIds = await getAllRoomIds()
    if(roomIds.length){
      GM_setValue(hotelIds.mother, roomIds);
    }else{
      alert('房型ID没有找到，不知道怎么回事 o(╥﹏╥)o')
    }
  }

  // 高亮房型元素
  function hightOurRooms(){
    let hotelId = location.pathname.match(/\d{6,12}/g)[0]
    let roomIds = GM_getValue(hotelId, [])
    if(roomIds.length){
      $.each($('div[ubt-roomid]'), (index, elem)=>{
        if(roomIds.indexOf($(elem).attr('ubt-roomid')) > -1){
          $(elem).addClass('my-room') // 高亮底色
          $(elem).find('.promotion').append(`<b>${$(elem).attr('ubt-roomid')}</b>`) // 添加房型ID，方便
        }
      })
    }else{
      alert("没有此酒店的房型ID ╮(╯▽╰)╭")
    }
  }
  // 酒店销售页面，添加高亮按钮
  function addHilightButton(){
    $('div.cfilterquick>ul').append('<li id="hightOurRooms" class="filter-tag" style="background:#B4FE0F;">高亮</li>')
    $('#hightOurRooms').on('click', ()=>{hightOurRooms()})
  }

  if(location.href.includes("hotels.ctrip.com/hotels/")){
    GM_addStyle('.my-room{background:#B4FE0F !important;}')
    GM_registerMenuCommand('添加高亮按钮', addHilightButton, 'H');
  }else{
    // 命令菜单注册
    GM_registerMenuCommand('更新房型ID', updateHotelRoomids, 'U');
  }

})